<template>
  <div class="execution">
    <basic-container>
            <avue-crud ref="crud"
                       :page="page"
                       :data="tableData"
                       :table-loading="tableLoading"
                       :option="tableOption"
                       @search-change="searchChange"
                       @current-change="currentChange"
                       @size-change="sizeChange"
                       @refresh-change="refreshChange"
                       @row-update="handleUpdate"
                       @row-save="handleSave"
                       @row-del="rowDel">
              <template slot="search">
                <el-form-item label="商铺列表" v-if="isShowShopList">
                  <avue-crud-select v-model="searchFrom.shopId"
                                    placeholder="请选择商铺"
                                    size="small"
                                    :dic="shopsOptions"
                                    :props="shopsProps"></avue-crud-select>
                </el-form-item>
              </template>
              <template slot="menuLeft">
                <el-button :loading="downloadLoading" size="small" type="primary" icon="el-icon-download"
                           @click="handleDownload">导出
                </el-button>
              </template>
              <template slot="drawStatus" slot-scope="scope">
                <el-tag v-if="scope.row.drawStatus==0">待审核</el-tag>
                <el-tag v-if="scope.row.drawStatus==1">待打款</el-tag>
                <el-tag v-if="scope.row.drawStatus==2">驳回</el-tag>
                <el-tag v-if="scope.row.drawStatus==3">已打款</el-tag>
              </template>
              <template slot-scope="scope"
                        slot="menu">
                <!--<el-button type="text"-->
                <!--v-if="permissions.delivery_deliverymandraw_edit"-->
                <!--icon="el-icon-check"-->
                <!--size="small"-->
                <!--plain-->
                <!--@click="handleEdit(scope.row,scope.index)">编辑</el-button>-->
                <el-button type="text"
                           v-if="scope.row.drawStatus===0"
                           icon="el-icon-check"
                           size="small"
                           plain
                           @click="handleAudit(scope.row,scope.index)">审核
                </el-button>
                <el-button type="text"
                           v-if="scope.row.drawStatus===1"
                           icon="el-icon-check"
                           size="small"
                           plain
                           @click="handlePayment(scope.row.id)">确认打款
                </el-button>
                <el-button type="text"
                           icon="el-icon-delete"
                           size="small"
                           plain
                           @click="handleDel(scope.row,scope.index)">删除
                </el-button>
              </template>
            </avue-crud>

    </basic-container>

    <el-dialog
      title="提现审核"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <span>确认该提现审核吗？</span>
<!--      <avue-form-detail v-model="form" :option="option"></avue-form-detail>-->
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleRefuse" size="small">不通过</el-button>
        <el-button type="primary" size="small" @click="handleApproval">通 过</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    fetchList,
    getObj,
    addObj,
    putObj,
    delObj,
    checkDrawPass,
    checkDrawReject,
    remittance
  } from '@/api/delivery/deliverymandraw'
  import {getAllShops} from '@/api/shop/shops'
  import {tableOption} from '@/const/crud/delivery/deliverymandraw'
  import {mapGetters} from 'vuex'

  export default {
    name: 'deliverymandraw',
    data() {
      return {
        isShowShopList: false,
        activeName: 'first',
        tableLoading: false,
        tableData: [],
        downloadLoading: false,
        searchFrom: {
          shopId: '',
          drawStatus: ''
        },
        shopsProps: {
          label: "shopName",
          value: 'id'
        },
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20 // 每页显示多少条
        },
        shopsOptions: [],
        tableOption: tableOption,
        dialogVisible: false,
        form: {},
        option: {
          option: [{
            column: [{
              label: '姓名',
              prop: 'deName'
            }, {
              label: '提现金额',
              prop: 'drawMoney'
            }]
          }]
        }
      }
    },
    created() {
    },
    mounted: function () {
      // this.getList(this.page, this.searchFrom)
      if (this.$store.state.user.shopId && this.$store.state.user.shopId !== '') {
        this.searchFrom.shopId = this.$store.state.user.shopId
        this.isShowShopList = false
      } else {
        this.isShowShopList = true
      }
      this.getAllShops()
    },
    // computed: {
    //   ...mapGetters(['permissions'])
    // },
    methods: {
      init(params) {
        if (params && params.drawStatus) {
          this.searchFrom.drawStatus = params.drawStatus
        }
        if (!params) {
          params = {
            shopId: ''
          }
        }
        if (this.searchFrom.shopId) {
          params.shopId =this.searchFrom.shopId
        }
        this.getList(this.page, params)
      },
      getList(page, params) {
        this.tableLoading = true
        fetchList(Object.assign({
          current: page.currentPage,
          size: page.pageSize
        }, params)).then(response => {
          this.tableData = response.data.data.records
          this.page.total = response.data.data.total
          this.tableLoading = false
        })
      },
      getAllShops() {
        const data = new FormData()
        data.append('shopId', this.$store.state.user.shopId)
        getAllShops(data)
          .then(res => {
            this.shopsOptions = res.data.data
          })
      },
      /**
       * @title 打开新增窗口
       * @detail 调用crud的handleadd方法即可
       *
       **/
      handleAdd: function () {
        this.$refs.crud.rowAdd()
      },
      handleEdit(row, index) {
        this.$refs.crud.rowEdit(row, index)
      },
      handleDel(row, index) {
        this.$refs.crud.rowDel(row, index)
      },
      rowDel: function (row, index) {
        var _this = this
        this.$confirm('是否确认删除ID为' + row.id, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          return delObj(row.id)
        }).then(data => {
          _this.tableData.splice(index, 1)
          _this.$message({
            showClose: true,
            message: '删除成功',
            type: 'success'
          })
          this.getList(this.page, {
            drawStatus: this.searchFrom.drawStatus
          })
        }).catch(function (err) {
        })
      },
      /**
       * @title 数据更新
       * @param row 为当前的数据
       * @param index 为当前更新数据的行数
       * @param done 为表单关闭函数
       *
       **/
      handleUpdate: function (row, index, done) {
        putObj(row).then(data => {
          this.tableData.splice(index, 1, Object.assign({}, row))
          this.$message({
            showClose: true,
            message: '修改成功',
            type: 'success'
          })
          done()
          this.getList(this.page, {
            drawStatus: this.searchFrom.drawStatus
          })
        })
      },
      /**
       * @title 数据添加
       * @param row 为当前的数据
       * @param done 为表单关闭函数
       *
       **/
      handleSave: function (row, done) {
        addObj(row).then(data => {
          this.tableData.push(Object.assign({}, row))
          this.$message({
            showClose: true,
            message: '添加成功',
            type: 'success'
          })
          done()
          this.getList(this.page, {
            drawStatus: this.searchFrom.drawStatus
          })
        })
      },
      /**
       * 刷新回调
       */
      refreshChange() {
        this.getList(this.page, this.searchFrom)
        // this.$emit('refreshList')
      },
      //审核
      handleAudit(row, index) {
        this.dialogVisible = true
        this.form = row
      },
      handleClose() {
        this.dialogVisible = false
      },

      //审核不通过
      handleRefuse() {
        const data = new FormData()
        data.append('id', this.form.id)
        // data.append('reason', value)
        checkDrawReject(data).then(res => {
          this.$message({
            showClose: true,
            message: '审核成功',
            type: 'success'
          })
          this.dialogVisible = false
          this.getList(this.page, {
            drawStatus: this.searchFrom.drawStatus
          })
        })
      },

      //审核通过
      handleApproval() {
        const data = new FormData()
        data.append('id', this.form.id)
        checkDrawPass(data).then(res => {
          this.$message({
            showClose: true,
            message: '申请已通过',
            type: 'success'
          })
          this.dialogVisible = false
          this.getList(this.page, {
            drawStatus: this.searchFrom.drawStatus
          })
        })
      },

      // 打款
      handlePayment(id) {
        this.$confirm('是否确认打款?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const data = new FormData()
          data.append('id', id)
          remittance(data).then(res => {
            this.$message({
              showClose: true,
              message: '已确认打款',
              type: 'success'
            })
            this.dialogVisible = false
            this.getList(this.page, {
              drawStatus: this.searchFrom.drawStatus
            })
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          });
        });
      },
      searchChange(params) {
        params.shopId = this.searchFrom.shopId
        params.drawStatus = this.searchFrom.drawStatus
        this.getList(this.page, params)
      },
      sizeChange(val) {
        this.page.currentPage = 1
        this.page.pageSize = val
        this.getList(this.page, this.searchFrom)
      },
      currentChange(val) {
        this.page.currentPage = val
        this.getList(this.page, this.searchFrom)
      },
      handleDownload() {
        this.downloadLoading = true
        import('@/vendor/Export2Excel').then(excel => {
          const filterVal = ['shopName', 'deName', 'dePhone', 'drawMoney', 'drawStatus', 'drawTime', 'drawCheckTime', 'drawRemitTime', 'reason']
          const tHeader = ['所属商铺', '配送员姓名', '配送员电话', '提现金额', '审核状态', '提现时间', '审核时间', '打款时间', '审核不通过原因']
          const data = this.formatJson(filterVal, this.tableData)
          excel.export_json_to_excel({
            header: tHeader,
            data,
            filename: '提现列表' + this.page.currentPage
          })
          this.downloadLoading = false
        })
      },
      formatJson(filterVal, jsonData) {
        return jsonData.map(v => filterVal.map(j => {
          if (j === 'drawStatus') {
            if (v[j] === 0) {
              return '待审核'
            } else if (v[j] === 1) {
              return '审核通过'
            } else if (v[j] === 2) {
              return '审核不通过'
            } else if (v[j] === 3) {
              return '已打款'
            } else {
              return ''
            }
          } else {
            return v[j]
          }
        }))
      }
    }
  }
</script>

<style lang="scss" scoped>
  .el-dialog__body {
    padding: 0 20px !important;
  }
</style>
